package cn.xy.base.server.controller.admapi;

import cn.xy.base.server.service.SysConfigService;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.annotation.RepeatSubmit;
import cn.xy.commons.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 表[sys_config]对应的后台管理类
 *
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/admapi/sys/config")
@Api(tags = {"配置管理接口"})
public class SysConfigAdminController {

    @Autowired
    private SysConfigService sysConfigService;

    @GetMapping(value = "/getConfig")
    @ApiOperation(value = "配置管理-获取配置")
    @PreAuthorize("@validate.hasPermission('system-config-get')")
    @OperateLog(description = "获取配置")
    public Result<String> getConfig() {
        String data = sysConfigService.getConfig(SysConfigService.STR, String.class);
        return Result.data(data);
    }

    @PostMapping(value = "/saveConfig")
    @ApiOperation(value = "配置管理-保存配置")
    @PreAuthorize("@validate.hasPermission('system-config-save')")
    @OperateLog(description = "保存配置")
    @RepeatSubmit
    public Result<String> saveConfig(@RequestBody @Validated String config) {
        sysConfigService.saveConfig(SysConfigService.STR, config);
        return Result.ok("保存成功");
    }

}
